package com.mars.admin.modules.app;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.mars.admin.common.request.activity.ActivityRequest;
import com.mars.admin.framework.common.Result;
import com.mars.admin.modules.activity.entity.Activity;
import com.mars.admin.modules.activity.service.IActivityService;
import com.mars.admin.modules.activity.vo.ActivityVO;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 活动控制器
 *
 * @author Mars
 * @version 1.0
 * @date 2025-01-07
 */
@Slf4j
@RestController
@RequestMapping("/api/activity")
@Tag(name = "活动管理", description = "活动相关接口")
@AllArgsConstructor
public class ActivityController {

    private final IActivityService activityService;

    /**
     * 获取推荐活动
     */
    @GetMapping("/recommend")
    @Operation(summary = "获取推荐活动", description = "获取推荐活动列表")
    public Result<List<ActivityVO>> getRecommendActivities(@RequestParam(defaultValue = "10") Integer limit) {
        List<ActivityVO> activities = activityService.getRecommendActivities(limit);
        return Result.success(activities);
    }

    /**
     * 获取热门活动
     */
    @GetMapping("/hot")
    @Operation(summary = "获取热门活动", description = "获取热门活动列表")
    public Result<List<Activity>> getHotActivities(@RequestParam(defaultValue = "10") Integer limit) {
        List<Activity> activities = activityService.getHotActivities(limit);
        return Result.success(activities);
    }

    /**
     * 获取置顶活动
     */
    @GetMapping("/top")
    @Operation(summary = "获取置顶活动", description = "获取置顶活动列表")
    public Result<List<Activity>> getTopActivities(@RequestParam(defaultValue = "5") Integer limit) {
        List<Activity> activities = activityService.getTopActivities(limit);
        return Result.success(activities);
    }

    /**
     * 根据分类获取活动
     */
    @GetMapping("/category/{categoryId}")
    @Operation(summary = "根据分类获取活动", description = "根据分类ID获取活动列表")
    @Parameter(name = "categoryId", description = "分类ID", required = true)
    public Result<List<ActivityVO>> getActivitiesByCategory(@PathVariable Long categoryId, @RequestParam(defaultValue = "1") Integer status) {
        List<ActivityVO> activities = activityService.getActivitiesByCategoryId(categoryId, status);
        return Result.success(activities);
    }

    /**
     * 分页查询活动
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询活动", description = "分页查询活动列表")
    public Result<Page<Activity>> page(@RequestBody ActivityRequest request) {
        Page<Activity> page = Page.of(request.getPageNumber(), request.getPageSize());
        Page<Activity> result = activityService.pageActivity(page, request);
        return Result.success(result);
    }

    /**
     * 根据ID查询活动详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询活动详情", description = "根据ID查询活动详情")
    @Parameter(name = "id", description = "活动ID", required = true)
    public Result<ActivityVO> getById(@PathVariable Long id) {
        return Result.success(activityService.getActivityById(id));
    }


    /**
     * 更新活动
     */
    @PutMapping
    @Operation(summary = "更新活动", description = "更新活动信息")
    @SaCheckLogin
    public Result<Void> update(@RequestBody Activity activity) {
        // 检查是否是活动发布者
        Activity existActivity = activityService.getById(activity.getId());
        if (existActivity == null) {
            return Result.error("活动不存在");
        }
        if (!existActivity.getUserId().equals(StpUtil.getLoginIdAsLong())) {
            return Result.error("无权限修改此活动");
        }

        boolean result = activityService.updateById(activity);
        return result ? Result.success() : Result.error("更新失败");
    }

    /**
     * 删除活动
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除活动", description = "根据ID删除活动")
    @Parameter(name = "id", description = "活动ID", required = true)
    @SaCheckLogin
    public Result<Void> delete(@PathVariable Long id) {
        // 检查是否是活动发布者
        Activity activity = activityService.getById(id);
        if (activity == null) {
            return Result.error("活动不存在");
        }
        if (!activity.getUserId().equals(StpUtil.getLoginIdAsLong())) {
            return Result.error("无权限删除此活动");
        }

        boolean result = activityService.removeById(id);
        return result ? Result.success() : Result.error("删除失败");
    }

    /**
     * 我发布的活动
     */
    @GetMapping("/my")
    @Operation(summary = "我发布的活动", description = "获取当前用户发布的活动列表")
    @SaCheckLogin
    public Result<List<Activity>> getMyActivities(@RequestParam(required = false) Integer status) {
        Long userId = StpUtil.getLoginIdAsLong();
        List<Activity> activities = activityService.getActivitiesByUserId(userId, status);
        return Result.success(activities);
    }

    /**
     * 审核活动
     */
    @PostMapping("/audit/{id}")
    @Operation(summary = "审核活动", description = "审核活动")
    @Parameter(name = "id", description = "活动ID", required = true)
    @SaCheckLogin
    public Result<Void> auditActivity(@PathVariable Long id, @RequestParam Integer auditStatus, @RequestParam(required = false) String auditReason) {
        Long auditUserId = StpUtil.getLoginIdAsLong();
        activityService.auditActivity(id, auditStatus, auditReason, auditUserId);
        return Result.success();
    }


    /**
     * 发布活动
     */
    @PostMapping
    @Operation(summary = "发布活动", description = "发布新活动")
    public Result<Void> save(@RequestBody ActivityRequest request) {
        request.setUserId(StpUtil.getLoginIdAsLong());
        boolean result = activityService.publish(request);
        return result ? Result.success() : Result.error("发布失败");
    }

    /**
     * 查询我自己发布的活动数量
     */
    @GetMapping("/my/count")
    @Operation(summary = "查询我自己发布的活动数量", description = "查询我自己发布的活动数量")
    public Result<Long> countMyActivities(Long userId) {
        long count = activityService.countMyActivities(Objects.nonNull(userId) ? userId : StpUtil.getLoginIdAsLong());
        return Result.success(count);
    }


}
